2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex2_masterRxSingle.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
55 
68 //
69 //*****************************************************************************
70 //*****************************************************************************
71 //
72 //Set the address for slave module. This is a 7-bit address sent in the
73 //following format:
74 //[A6:A5:A4:A3:A2:A1:A0:RS]
75 //
76 //A zero in the "RS" position of the first byte means that the master
77 //transmits (sends) data to the selected slave, and a one in this position
78 //means that the master receives data from the slave.
79 //
80 //*****************************************************************************
81 
82 #define SLAVE_ADDRESS 0x48
83 
84 uint8_t RXData;
85 void main (void)
86 {
87  //Stop WDT
88  WDT_A_hold(WDT_A_BASE);
89 
90  //Set DCO frequency to 8MHz
91  CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_3);
92  //Set ACLK = DCO with frequency divider of 8
93  CS_initClockSignal(CS_ACLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
94  //Set SMCLK = DCO with frequency divider of 8
95  CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
96  //Set MCLK = DCO with frequency divider of 8
97  CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_8);
98 
99  // Configure Pins for I2C
100  //Set P1.6 and P1.7 as Secondary Module Function Input.
101  /*
102 
103  * Select Port 1
104  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
105  */
106  GPIO_setAsPeripheralModuleFunctionInputPin(
107  GPIO_PORT_P1,
108  GPIO_PIN6 + GPIO_PIN7,
109  GPIO_SECONDARY_MODULE_FUNCTION
110  );
111 
112  /*
113  * Disable the GPIO power-on default high-impedance mode to activate
114  * previously configured port settings
115  */
116  PMM_unlockLPM5();
117 
118  EUSCI_B_I2C_initMasterParam param = {0};
119  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
120  param.i2cClk = CS_getSMCLK();
121  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
122  param.byteCounterThreshold = 1;
123  param.autoSTOPGeneration = EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD;
124  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
125 
126  //Specify slave address
127  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
129  );
130 
131  //Set Master in receive mode
132  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
133  EUSCI_B_I2C_RECEIVE_MODE
134  );
135 
136  //Enable I2C Module to start operations
137  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
138 
139  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
140  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
141  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
142  );
143 
144 
145  while (1)
146  {
147  __delay_cycles(2000);
148 
149  // I2C start condition
150  RXData = EUSCI_B_I2C_masterReceiveSingleByte(EUSCI_B0_BASE);
151 
152  }
153 }
void main(void)
#define SLAVE_ADDRESS
MPU_initThreeSegmentsParam param
__delay_cycles(500000)